package com.morehair.controller.wx;

import com.morehair.bean.bo.wx.WxAddGoodsToCartBo;
import com.morehair.bean.bo.wx.WxCheckedCartBo;
import com.morehair.bean.bo.wx.WxDeleteCartGoodsBo;
import com.morehair.bean.bo.wx.WxUpdateCartOfGoodsBo;
import com.morehair.bean.pojo.User;
import com.morehair.bean.vo.admin.BaseRespVo;
import com.morehair.bean.vo.wx.WxCartIndexVo;
import com.morehair.bean.vo.wx.WxCheckoutVo;
import com.morehair.service.wx.WxCartService;
import com.morehair.utils.GetNowUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/8 13:30
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService wxCartService;

    /**
     * 个人购物车里商品展示（index）
     * @return
     */
    @GetMapping("index")
    public BaseRespVo indexCart() {
        if (GetNowUser.getUser().getId()== null) {
            return  BaseRespVo.fail(-1,null,"亲，请先登录哦！");
        }
        WxCartIndexVo wxCartIndexVo=wxCartService.indexCart();
        return  BaseRespVo.ok(wxCartIndexVo);
    }

    /**
     * 获取购物车商品数量（包含选中和没选中的）
     * @return
     */
    @GetMapping("goodscount")
    public  BaseRespVo  getCartGoodsCount() {
        int count=0;
        if (GetNowUser.getUser()== null) {
            return  BaseRespVo.ok(count);
        }
        count=wxCartService.getCartGoodsCount();
        return  BaseRespVo.ok(count);
    }

    /**
     * 添加商品到购物车
     * @param wxAddGoodsToCartBo
     * @return
     */
    @PostMapping("add")
    public BaseRespVo addGoodsToCart(@RequestBody WxAddGoodsToCartBo wxAddGoodsToCartBo) {
        if (GetNowUser.getUser().getId()== null) {
            return  BaseRespVo.fail(-1,null,"亲，请先登录哦！");
        }
        int goodsCount=wxCartService.addGoodsToCart(wxAddGoodsToCartBo);
        if (goodsCount == 404) {
            return  BaseRespVo.fail(-1,null,"亲，库存不足，请联系商家客服！");
        }
        return  BaseRespVo.ok(goodsCount);
    }


    /**
     * 更新购物车商品（单条商品记录的数量增减）
     * @param wxUpdateCartOfGoodsBo
     * @return
     */
    @PostMapping("update")
    public  BaseRespVo  updateCart(@RequestBody  WxUpdateCartOfGoodsBo wxUpdateCartOfGoodsBo) {
         if (GetNowUser.getUser().getId()== null) {
            return  BaseRespVo.fail(-1,null,"亲，请先登录哦！");
        }
        int updateNumber=wxCartService.updateCart(wxUpdateCartOfGoodsBo);
        if (updateNumber == 404) {
            return  BaseRespVo.fail(-1,null,"亲，库存不足，请联系商家客服！");
        } else if (updateNumber == 500) {
            return  BaseRespVo.fail(-1,null,"服务器繁忙，请稍后再试");
        }
        return  BaseRespVo.ok("成功");
    }

    /**
     * 删除购物车的商品
     * @param wxDeleteCartGoodsBo
     * @return
     */
    @PostMapping("delete")
    public  BaseRespVo  deleteCart(@RequestBody WxDeleteCartGoodsBo wxDeleteCartGoodsBo) { //复用WxCartIndexVo
        if (GetNowUser.getUser().getId()== null) {
            return  BaseRespVo.fail(-1,null,"亲，请先登录哦！");
        }
        WxCartIndexVo wxCartIndexVo=wxCartService.deleteCart(wxDeleteCartGoodsBo);
        return  BaseRespVo.ok(wxCartIndexVo);
    }

    /**
     * 选择或取消选择商品
     * @param wxCheckedCartBo
     * @return
     */
    @PostMapping("checked")
    public  BaseRespVo  checkedCart(@RequestBody WxCheckedCartBo wxCheckedCartBo) {
        if (GetNowUser.getUser().getId()== null) {
            return  BaseRespVo.fail(-1,null,"亲，请先登录哦！");
        }
        WxCartIndexVo wxCartIndexVo=wxCartService.checkedCart(wxCheckedCartBo);
        return  BaseRespVo.ok(wxCartIndexVo);
    }


    /**
     * 下单前信息确认
     * @return
     */
    @GetMapping("checkout")
    public  BaseRespVo  checkoutOrder(Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId) {
        if (GetNowUser.getUser().getId()== null) {
            return  BaseRespVo.fail(-1,null,"亲，请先登录哦！");
        }
        WxCheckoutVo wxCheckoutVo=wxCartService.checkoutOrder(cartId, addressId, couponId, grouponRulesId);
        return  BaseRespVo.ok(wxCheckoutVo);
    }

    //点击立即购买商品
    @PostMapping("fastadd")
    public  BaseRespVo  fastaddGoods(@RequestBody WxAddGoodsToCartBo wxAddGoodsToCartBo) {
        if (GetNowUser.getUser().getId()== null) {
            return  BaseRespVo.fail(-1,null,"亲，请先登录哦！");
        }
        //返回的是cart.id
        Integer cartId=wxCartService.fastaddGoods(wxAddGoodsToCartBo);
        if (cartId == -1) {
            return  BaseRespVo.fail(-1,null,"亲，请先登录哦！");
        } else if (cartId==-200) {
            return  BaseRespVo.fail(-1,null,"亲，库存不足，请联系商家客服！");
        }
        return  BaseRespVo.ok(cartId);
    }

}
